package com.wu.controller;

import com.wu.pojo.view.ArticleSearch;
import com.wu.response.CommonReturnType;
import com.wu.service.ArticleSearchService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;

import static com.wu.constant.Constant.DEFAULT_NULL_VALUE;
import static com.wu.constant.Constant.ONE;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-05-07 11:42
 */
@RestController
@Api(tags = "搜索")
public class SearchController {

    @Autowired
    private ArticleSearchService articleSearchService;

    /**
     * 通过文章标题搜索文章标题
     *
     * @param articleTitle
     * @return
     */
    @ApiOperation(value = "通过文章标题搜索文章标题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleTitle", value = "文章标题", dataTypeClass = String.class)
    })
    @RequestMapping(value = "/searchArticle", method = RequestMethod.POST)
    public CommonReturnType searchArticleTile(@RequestParam(name = "articleTitle") String articleTitle) {
//        List<ArticleSearch> article = articleSearchService.getArticleByArticleTitle(articleTitle);
        List<ArticleSearch> article = articleSearchService.getArticleByArticleTitleByLucence(articleTitle);
        return CommonReturnType.create(article);
    }

    /**
     * 当用户回车后的搜索事件
     *
     * @param articleTitle
     * @param p
     * @return
     */
    @ApiOperation(value = "当用户回车后的搜索事件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleTitle", value = "文章标题", dataTypeClass = String.class),
            @ApiImplicitParam(name = "p", value = "当前页", dataTypeClass = Integer.class)
    })
    @RequestMapping(value = "/searchLikeArticle", method = RequestMethod.POST)
    public CommonReturnType searchArticle(@RequestParam(name = "articleTitle") String articleTitle,
                                          @RequestParam(name = "p", required = false) Integer p) {
        HashMap<String, Object> likeArticle = articleSearchService.getLikeArticle(articleTitle, p);
        return CommonReturnType.create(likeArticle);
    }

    /**
     * 根据用户Id和文章标题搜索用户自己的文章标题
     *
     * @param articleTitle
     * @param userId
     * @return
     */
    @ApiOperation(value = "根据用户Id和文章标题搜索用户自己的文章标题")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleTitle", value = "文章标题", dataTypeClass = String.class),
            @ApiImplicitParam(name = "userId", value = "用户Id", dataTypeClass = Integer.class)
    })
    @RequestMapping(value = "/searchUserBlogTitle", method = RequestMethod.POST)
    public CommonReturnType searchUserBlogTitle(@RequestParam(name = "articleTitle") String articleTitle,
                                                @RequestParam(name = "userId") String userId
    ) {
        List<ArticleSearch> articleByArticleTitle = articleSearchService.getArticleByArticleTitle(articleTitle, userId);
        return CommonReturnType.create(articleByArticleTitle);
    }

    /**
     * 用户的回车事件(查询指定用户的文章)
     *
     * @param articleTitle
     * @param p
     * @return
     */
    @ApiOperation(value = "用户的回车事件(或查询指定用户的文章)")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "articleTitle", value = "文章标题", dataTypeClass = String.class),
            @ApiImplicitParam(name = "p", value = "当前页", defaultValue = ONE, dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "userId", value = "用户Id，如果为空就查全部", dataTypeClass = String.class)
    })
    @RequestMapping(value = "/searchLikeUserArticle", method = RequestMethod.POST)
    public CommonReturnType searchLikeUserArticle(@RequestParam(name = "articleTitle") String articleTitle,
                                                  @RequestParam(name = "p", required = false, defaultValue = ONE) Integer p,
                                                  @RequestParam(name = "userId", required = false, defaultValue = DEFAULT_NULL_VALUE) String userId) {
        HashMap<String, Object> likeArticle = articleSearchService.getLikeArticle(articleTitle, p, userId);
        return CommonReturnType.create(likeArticle);
    }
}
